<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <script>
<<<<<<< HEAD
        //树形数据处理为线性数据
        var pdata = [
=======
    // 转换前  参数说明：name：对象的名字，catId ： 对象的ID（唯一值）
    // 转换后  参数说明: name：对象的名字，catId ： 对象的ID（唯一值） pid ：父亲的ID
      var pdata = [
>>>>>>> d27aa3f976f692ab7be34e4998fad5fce2887964
            {
                name:"章三",
                catId:1,
                children:[
                    {
                        catId:"4",
                        name:"章三的大儿子",
                        children:[
                            {
                                catId:"6",
                                name:"章三的孙子"
                            }
                        ]
                    },
                    {
                        catId:"5",
                        name:"章三的小儿子"
                    }
                ]
            },
            {
                name:"王五",
                catId:2,
                children:[
                    {
                        name:"王五的孩子",
                        catId:3
                    }
                ]
            }
        ]    
<<<<<<< HEAD
=======

>>>>>>> d27aa3f976f692ab7be34e4998fad5fce2887964
    function renderData(pdata,pid){
        if (pdata.length == 0) {
            return false
        }
        var arr = new Array();
        var linarr = []
        for (let i = 0; i < pdata.length; i++) {
            var ele = pdata[i]
            var obj = {
                name:ele.name,
                catId:ele.catId,
                pid:pid
            };
            arr.push(obj)
            if (ele.children && ele.children.length > 0) {
                linarr = renderData(ele.children,ele.catId)
                arr.push(...linarr)
            }
        }
        return arr
    }
    console.log(renderData(pdata,0))
    </script>
</body>
</html>